//
//  HYFAutoProgressView.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/20.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

class HYFAutoProgressView: UIProgressView {
    
    //定义一个 定时器
    var timer:NSTimer?
    
    //定义一个 bool 值,是否动画
    var isAnim:Bool = true

    override init(frame: CGRect) {
        
        super.init(frame: CGRect(x: 0, y: 64, width: SCREENW, height: 2))
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
        //MARK:设置视图
    private func setupUI(){
        
        self.tintColor = UIColor.greenColor()
    }
    
    
    //开始动画
    func startAnim(){
        
        //开始动画的 时候, 不要隐藏  self.hidden = false
        self.progress = 0
        
        self.hidden = false
        
        timer = NSTimer.scheduledTimerWithTimeInterval(1/60, target: self, selector: #selector(HYFAutoProgressView.valueChange), userInfo: nil, repeats: true)
        
    }
    
    //删除当前控件
    func removeView(){
        removeFromSuperview()
    }
    
    //监控方法
    func valueChange(){
        
        if isAnim {
            
            self.progress += 0.05
            
            if self.progress >= 0.95 {
                
                self.progress = 0.95
            }
            
        }else{
            
            if self.progress == 1 {
            
                self.hidden = true
                
                timer?.invalidate()
                
            }else{
                
                self.progress = 1
            }
        }
    }
    
    //结束动画
    func endAnim(){
        
        isAnim = false
    }
    
    deinit {
        timer?.invalidate()
    }
}
